package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.ObjectUtil;
import com.zhijian.medical.dao.local.AgentiaCheckHistoryMapper;
import com.zhijian.medical.entity.po.AgentiaCheckHistory;
import com.zhijian.medical.entity.vo.request.AgentiaCheckHistorySearchVo;
import com.zhijian.medical.entity.vo.response.AgentiaCheckHistoryVo;
import com.zhijian.medical.service.AgentiaCheckHistoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 试剂-分子病理室试剂验收试验记录表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-07-21 15:11:37
 */
@Slf4j
@Service
public class AgentiaCheckHistoryServiceImpl implements AgentiaCheckHistoryService {

    @Autowired
    private AgentiaCheckHistoryMapper mapper;

    @Override
    public AgentiaCheckHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AgentiaCheckHistory selectByAgentiaId(Long agentiaId) {
        return mapper.selectByAgentiaId(agentiaId);
    }

    @Override
    public AgentiaCheckHistoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(AgentiaCheckHistory agentiaCheckHistory) {
        Date now = new Date();
        agentiaCheckHistory.setCreateTime(now);
        agentiaCheckHistory.setUpdateTime(now);
        return mapper.insert(agentiaCheckHistory) > 0 ? agentiaCheckHistory.getId() : 0L;
    }

    @Override
    public boolean saveOrUpdate(AgentiaCheckHistory agentiaCheckHistory) {
        AgentiaCheckHistory old = this.selectByAgentiaId(agentiaCheckHistory.getAgentiaId());
        if (ObjectUtil.isNull(old)) {
            //保存
            return this.insert(agentiaCheckHistory) != 0;
        } else {
            //修改
            agentiaCheckHistory.setId(old.getId());
            return this.update(agentiaCheckHistory);
        }
    }

    @Override
    public boolean update(AgentiaCheckHistory agentiaCheckHistory) {
        Date now = new Date();
        agentiaCheckHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(agentiaCheckHistory) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AgentiaCheckHistory record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AgentiaCheckHistorySearchVo agentiaCheckHistorySearchVo) {
        return mapper.count(agentiaCheckHistorySearchVo);
    }

    @Override
    public List<AgentiaCheckHistoryVo> selectList(AgentiaCheckHistorySearchVo agentiaCheckHistorySearchVo) {
        return mapper.selectList(agentiaCheckHistorySearchVo);
    }

    @Override
    public Page<AgentiaCheckHistoryVo> selectPage(AgentiaCheckHistorySearchVo agentiaCheckHistorySearchVo) {
        //先直接查总数
        int count = count(agentiaCheckHistorySearchVo);
        //分页
        Page<AgentiaCheckHistoryVo> page = new Page<>(true, count, agentiaCheckHistorySearchVo.getPageNum(), agentiaCheckHistorySearchVo.getPageSize());
        agentiaCheckHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AgentiaCheckHistoryVo> responseVos = this.selectList(agentiaCheckHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AgentiaCheckHistory> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    public static final String CODE_PREFIX = "FZ-SOP-C-001-02";

    @Override
    public String selectMaxCode() {
        return mapper.selectMaxCodeByDay(DateUtil.formatDate(new Date()));
    }

    @Override
    public String createNextCode() {
        String maxCode = this.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return String.format("%s-%s-1", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")));
        }
        String[] maxCodeArray = maxCode.split("-");
        Integer i = Integer.valueOf(maxCodeArray[maxCodeArray.length - 1]);
        i++;
        return String.format("%s-%s-%s", CODE_PREFIX, DateUtil.format(new Date(), FastDateFormat.getInstance("yyyyMMdd")), i);
    }

}